package com.augmentum.login.util;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger;

import com.augmentum.login.AppContent;
import com.augmentum.login.model.User;
import com.augmentum.login.service.impl.QuestionServiceImpl;
//@Service("questionLog")
public class LogMethod implements MethodInterceptor {
    private final Logger logger = Logger.getLogger(QuestionServiceImpl.class);
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object returnValue = invocation.proceed();
        String methodName = invocation.getMethod().getName();
        long endTime = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder();
        User user = AppContent.getAppContent().getUser();
        if(user != null) {
            sb.append(user.toString());
        }
        sb.append(":");
        sb.append(invocation.getMethod().getDeclaringClass().getSimpleName());
        sb.append(":");
        sb.append(methodName);
        sb.append(":");
        sb.append(endTime - startTime);
        logger.info(sb.toString());
        return returnValue;
    }

}
